package translate;

import java.util.ArrayList;

import tree.BasicExp;
import tree.BasicStm;
import tree.CONST;
import tree.ESEQ;
import tree.EXP;
import tree.SEQ;

public class SeqExp extends Ex {
	ArrayList<Exp> seq = new ArrayList<Exp>();
	
	public SeqExp() {
		super(null);
	}
	
	public void addStm(Exp s) {
		seq.add(s);
	}
	
	BasicExp unEx() {
		SEQ head, tail;

		if (seq.size() == 1)
			return seq.get(0).unEx();
		else if (seq.size() == 2)
			return new ESEQ(seq.get(0).unNx(), seq.get(1).unEx());
		else if (seq.size() > 2) {
			head = tail = new SEQ(seq.get(0).unNx(), null);
			for (int i = 1; i < seq.size() - 2; i++) {
				tail.right = new SEQ(seq.get(i).unNx(), null);
				tail = (SEQ)tail.right;
			}
			tail.right = seq.get(seq.size() - 2).unNx();
			return new ESEQ(head, seq.get(seq.size() - 1).unEx());
		}
		else return new CONST(0);
	}
	
	BasicStm unNx() {
		SEQ head, tail;
		
		if (seq.size() == 1)
			return seq.get(0).unNx();
		else if (seq.size() >= 2) {
			head = tail = new SEQ(seq.get(0).unNx(), null);
			for (int i = 1; i < seq.size() - 1; i++) {
				tail.right = new SEQ(seq.get(i).unNx(), null);
				tail = (SEQ)tail.right;
			}
			tail.right = seq.get(seq.size() - 1).unNx();
			return head;
		}
		else return new EXP(new CONST(0));
	}
}
